package se.mbaeumer.fxtodo.gui;

import se.mbaeumer.fxtodo.core.main.FXTodoMain;
import se.mbaeumer.fxtodo.core.model.Task;
import se.mbaeumer.fxtodo.core.util.constants.ContextConstants;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Orientation;
import javafx.geometry.Pos;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.layout.FlowPane;
import javafx.scene.paint.Color;
import javafx.stage.Modality;
import javafx.stage.Stage;

public class TaskMoveStage extends Stage{

	private Group root = new Group();
	private Scene scene;
	private FlowPane flowGeneral;
	private FlowPane flowOld;
	private FlowPane flowNew;
	private FlowPane flowButtons;
	private ComboBox<String> cmbContexts;
	private Label lblOldContextText;
	private Label lblOldContext;
	private Label lblNewContextText;
	private Label lblNewContext;
	private Button btnDiscard;
	private Button btnClose;
	
	private String oldContext;	
	private FXTodoMain fxtodoMain;
	private Task selectedTask;
	
	public TaskMoveStage(FXTodoMain fxtodoMain, Task selectedTask){
		this.fxtodoMain = fxtodoMain;
		
		this.selectedTask = selectedTask;
		if (this.selectedTask.getTaskContext() != null){
			this.oldContext = this.selectedTask.getTaskContext().getTitle();
		}else{
			this.oldContext = ContextConstants.CONTEXT_UNKNOWN;
		}
		this.scene = new Scene(this.root, 200, 150, Color.BEIGE);
		this.initModality(Modality.APPLICATION_MODAL);
		this.setTitle("Move task");
		
		this.setScene(this.scene);
		this.initLayout();
		
		this.lblNewContext.textProperty().bind(this.cmbContexts.valueProperty());
	}
	
	private void initLayout(){
		this.createGeneralFlowPane();
		this.createOldFlowPane();
		this.createNewFlowPane();
		this.createContextComboBox();
		this.createButtonFlowPane();
	}
	
	private void createGeneralFlowPane(){
		this.flowGeneral = new FlowPane();
		this.flowGeneral.setOrientation(Orientation.VERTICAL);
		this.flowGeneral.setVgap(15);
		this.root.getChildren().add(this.flowGeneral);
	}
	
	private void createOldFlowPane(){
		this.flowOld = new FlowPane();
		this.flowOld.setOrientation(Orientation.HORIZONTAL);
		this.flowOld.setHgap(15);
		this.flowGeneral.getChildren().add(this.flowOld);
		this.createOldLabels();
	}
	
	private void createOldLabels(){
		this.lblOldContextText = new Label("Old: ");
		this.lblOldContext = new Label(this.oldContext);
		this.lblNewContextText = new Label("New: ");
		this.lblNewContext = new Label();
		
		this.flowOld.getChildren().addAll(this.lblOldContextText, this.lblOldContext);
	}
	
	private void createNewFlowPane(){
		this.flowNew = new FlowPane();
		this.flowNew.setOrientation(Orientation.HORIZONTAL);
		this.flowNew.setHgap(15);
		this.flowGeneral.getChildren().add(this.flowNew);
		this.createNewLabels();
	}
	
	private void createNewLabels(){
		this.lblNewContextText = new Label("New: ");
		this.lblNewContext = new Label();
		
		this.flowNew.getChildren().addAll(this.lblNewContextText, this.lblNewContext);
	}
	
	private void createContextComboBox(){
		FlowPane flowcontext = new FlowPane();
		flowcontext.setOrientation(Orientation.HORIZONTAL);
		flowcontext.setAlignment(Pos.CENTER_LEFT);
		this.setResizable(false);
		this.flowGeneral.getChildren().add(flowcontext);
		this.cmbContexts = new ComboBox<String>();
		this.setAvailableContexts();
		this.cmbContexts.valueProperty().addListener(new ChangeListener<String>() {

			@SuppressWarnings("rawtypes")
			@Override
			public void changed(ObservableValue arg0, String arg1,
					String arg2) {
				// TODO Auto-generated method stub
				fxtodoMain.moveTask(selectedTask, cmbContexts.getValue());
			}
		});
		
		flowcontext.getChildren().add(this.cmbContexts);
		
	}
	
	private void setAvailableContexts(){
		if (this.cmbContexts.getItems() != null){
			this.cmbContexts.getItems().clear();
		}
		this.cmbContexts.getItems().add(ContextConstants.CONTEXT_UNKNOWN);
		
		// set the initial value of the combobox
		if (this.selectedTask.getTaskContext() != null){
			this.cmbContexts.setValue(this.selectedTask.getTaskContext().getTitle());
		}else{
			this.cmbContexts.setValue(ContextConstants.CONTEXT_UNKNOWN);
		}
		
		// load the contexts from the database
		this.cmbContexts.getItems().addAll(FXCollections.observableList(this.fxtodoMain.loadContextTitlesFromDB()));
	}
	
	private void createButtonFlowPane(){
		this.flowButtons = new FlowPane();
		this.flowButtons.setOrientation(Orientation.HORIZONTAL);
		this.flowButtons.setHgap(10);
		
		this.flowGeneral.getChildren().add(this.flowButtons);
		this.createButtons();
	}
	
	private void createButtons(){
		this.btnDiscard = new Button("Discard");
		this.btnClose = new Button("Close");
	
		this.btnDiscard.setOnAction(new EventHandler<ActionEvent>() {

			@Override
			public void handle(ActionEvent arg0) {
				cmbContexts.setValue(oldContext);
				fxtodoMain.moveTask(selectedTask, cmbContexts.getValue());
			}
		});
		
		this.btnClose.setOnAction(new EventHandler<ActionEvent>() {

			@Override
			public void handle(ActionEvent arg0) {
				close();
			}
		});
		this.flowButtons.getChildren().addAll(this.btnDiscard, this.btnClose);
	}
}
